<?php

/*
  -----------------------------------------------------------------
  Copyright (c) 2010 25Solutions   All Rights Reserved.

  This software is the confidential and proprietary information
  of 25Solutions ("Confidential Information").
  You shall not disclose such Confidential Information and shall
  use it only in accordance with the terms of the license
  agreement you entered into with 25Solutions
  -----------------------------------------------------------------
 */

/**
 * Description of TfMailsMessage
 *
 * @author Fabio Lorenzini <fabio.lorenzini at 25solutions.com>
 */
class TfMailsMessage extends Swift_Message {

    /**
     * Prepares the mail to be sent to the Recipient passed as argument
     * @param Template $template
     * @param Recipient $recipient
     */
    public function __construct(Template $template, Recipient $recipient, $clickBaseUrl, $campaignId = 0) {
        parent::__construct();
        if ($template->getSenderName() != '') {
            $this->setFrom(array($template->getSenderAddress() => $template->getSenderName()));
        } else {
            $this->setFrom($template->getSenderAddress());
        }
        $this->setSubject(TfStringUtils::replacePlaceHolders($template->getSubject(), $recipient))
                ->setTo(array(trim($recipient->getEmail()) => $recipient->getNameToShow()));
        if ($template->getCc() != '') {
            $this->addCc($template->getCc());
        }
        if ($template->getBcc() != '') {
            $this->addBcc($template->getBcc());
        }
        $hasHtml = false;
        if ($template->getBodyHtml() != '') {
            $html = TfStringUtils::replaceViewOnlineLink($template->getBodyHtml(), $recipient, $campaignId);
            $hasHtml = true;
            $this->setBody(TfStringUtils::replaceLinks(TfStringUtils::replacePlaceHolders($html, $recipient), $recipient, $template, $clickBaseUrl, $campaignId), 'text/html'); //html message
        }
        if ($template->getBodyTxt() != '') {
                 

            $text = TfStringUtils::replaceViewOnlineLink($template->getBodyTxt(), $recipient, $campaignId);
            if ($hasHtml) {
                $this->addPart(TfStringUtils::replacePlaceHolders($text, $recipient), 'text/plain'); //text message
            } else {
                $this->setBody(TfStringUtils::replacePlaceHolders($text, $recipient), 'text/plain');
            }
        }
    }

}
?>
